/*
 * MembersDao.java
 *
 * Created on November 9, 2008, 7:39 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jollylengkono.skripsi.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jollylengkono.skripsi.iface.Crud;
import jollylengkono.skripsi.model.Members;

/**
 *
 * @author JollkY
 */
public class MembersDao implements Crud {
    
    private int state;
    private Members member;
    private List members;
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("kmPU");
    private EntityManager em = emf.createEntityManager();
    
    /** Creates a new instance of MembersDao */
    public MembersDao() {
    }

    
    public int create(Object obj) {
        try {
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }

    
    public List<Object> getList() {
        members = new ArrayList();
        try {
            em.getTransaction().begin();
            members = em.createQuery("SELECT m FROM Members m").getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return members;
    }
    
    public List getListByDepartment(long idDepartment) {
        members = new ArrayList();
        try {
            em.getTransaction().begin();
            members = em.createQuery("SELECT m FROM Members m " +
                    "WHERE m.department.id="+idDepartment).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return members;
    }

    public List getListByDivision(long idDivision) {
        members = new ArrayList();
        try {
            em.getTransaction().begin();
            members = em.createQuery("SELECT m FROM Members m " +
                    "WHERE m.division.id="+idDivision).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return members;
    }
    
    public Members signIn(Members m) {
        member = new Members();
        try {
            em.getTransaction().begin();
            member = (Members) em.createQuery("SELEC m FROM Members m " +
                    "WHERE m.username='"+m.getUsername()+"' AND m.password='"+m.getPassword()+"'").getSingleResult();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return member;
    }
    
    public int signOut(String memberSessionName) {
        return 0; 
    }
    
    public List getListByGroup(long idGroup) {
        members = new ArrayList();
        try {
            em.getTransaction().begin();
            members = em.createQuery("SELECT m FROM Members m " +
                    "WHERE m.groups.id="+idGroup).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return members;
    }
    
    
    public Object read(long id) {
        member = new Members();
        try {
            em.getTransaction().begin();
            member = em.find(Members.class, id);
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return member;
    }

    
    public int update(Object obj) {
        member = new Members();
        try {
            Members objMember = (Members) obj;
            em.getTransaction().begin();
            member = em.find(Members.class, objMember.getId());
            member.setCollaborativeWork(objMember.getCollaborativeWork());
            member.setDepartment(objMember.getDepartment());
            member.setDivision(objMember.getDivision());
            member.setEmail(objMember.getEmail());
            member.setFirstName(objMember.getFirstName());
            member.setGroup(objMember.getGroup());
            member.setLastActivity(objMember.getLastActivity());
            member.setLastName(objMember.getLastName());
            member.setPassword(objMember.getPassword());
            member.setStatus(objMember.getStatus());
            member.setUsername(objMember.getUsername());
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }

    
    public int delete(long id) {
        member = new Members();
        try {
            em.getTransaction().begin();
            member = em.find(Members.class, id);
            em.remove(member);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }
    
}
